<?php
/**
 * 系统出入日志
 * (model)
 */
namespace App\Models\Sys;

use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Model;
class Logs extends Model
{
    protected $table = 'sys_user_logs';

    protected $guarded = [];

    const PURCHASE      = 'purchase'; //采购单
    const FBA_DELIVERY  = 'fba-delivery'; //FBA发货单
    const P_DELIVERY    = 'p-delivery'; //非FBA发货单
    const OS_DELIVERY   = 'os-delivery'; //入仓非FBA发货单
    const OS_OUT        = 'os-out'; //出仓FBA发货单
    const OS_P_OUT      = 'os-p-out'; //出仓非FBA发货单
    const OS_IN_DELIVERY  = 'os-in-delivery'; //海外-(中转)入仓
    const OS_OUT_DELIVERY = 'os-out-delivery'; //海外-(中转)出仓
    const OS_IN_DROPSHIPPING_DELIVERY  = 'os-in-dropshipping-delivery'; //海外-(代发)入仓
    const OS_OUT_DROPSHIPPING_DELIVERY = 'os-out-dropshipping-delivery'; //海外-(代发)出仓

    const OS_F_OUT      = 'os-f-out'; //移库出仓
    const P_DELIVERY_A  = 'p-delivery-a'; //站外申请发货单

    const CUSTOMER_ORDER        = 'customer-order'; //客户订单
    const CUSTOMER_ORDER_FBA    = 'customer-order-fba'; //客户订单发货-FBA
    const CUSTOMER_ORDER_ABROAD = 'customer-order-abroad'; //客户订单发货-海外仓
    const CUSTOMER_ORDER_CN     = 'customer-order-cn'; //客户订单发货-CN
    const PICKUP_ORDER          = 'pickup-order'; //提货单

    public static $_type = [
        'purchase' => '采购单',
        'fba-delivery' => 'FBA发货单',
        'p-delivery' => '非FBA发货单',
        'os-delivery' => '入仓非FBA发货单',
        'os-out' => '出仓FBA发货单',
        'os-p-out' => '出仓非FBA发货单',
        'os-in-delivery' => '海外-(中转)入仓',
        'os-out-delivery' => '海外-(中转)出仓',
        'os-in-dropshipping-delivery' => '海外-(代发)入仓',
        'os-out-dropshipping-delivery' => '海外-(代发)出仓',
        'os-f-out' => '移库出仓',
        'p-delivery-a' => '站外申请发货单',
        'customer-order' => '客户订单',
        'customer-order-fba' => '客户订单发货-FBA',
        'customer-order-abroad' => '客户订单发货-海外仓',
        'customer-order-cn' => '客户订单发货-CN',
        'pickup-order' => '提货单',
    ];

    public static function logs($type,$content,$relatedId,$userId = null){
        return self::query()->create([
            'type' => $type,
            'content' => $content,
            'related_id' => $relatedId,
            'user_id' => $userId ?: Admin::user()->id
        ]);
    }

    public static function getLogs($type,$relatedId){
        return self::query()
            ->where('type',$type)
            ->where('related_id',$relatedId)
            ->orderBy('id','DESC')
            ->get();
    }
}
